<template>
  <div class="channel-edit">
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button
        class="edit-btn"
        size="mini"
        round
        type="danger"
        plain
        @click="isEdit = !isEdit"
        >{{ isEdit ? "完成" : "编辑" }}</van-button
      >
    </van-cell>
    <van-grid class="my-grid" :gutter="10">
      <van-grid-item
        icon="clear"
        class="grid-item"
        v-for="(channel, index) in userChannels"
        :key="index"
        @click="onMyChannelClick(channel, index)"
      >
        <template #icon>
          <van-icon
            name="clear"
            v-show="isEdit && !fiexdChannels.includes(channel.id)"
          ></van-icon>
        </template>

        <template #text
          ><span class="text" :class="{ active: index === activeIndex }">{{
            channel.name
          }}</span></template
        >
      </van-grid-item>
    </van-grid>
    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>
    <van-grid class="recomment-grid" :gutter="10">
      <van-grid-item
        icon="plus"
        class="grid-item"
        v-for="channel in recommendChannele"
        :key="channel.id"
        :text="channel.name"
        @click="onAddChannel(channel)"
      />
    </van-grid>
  </div>
</template>

<script>
import {
  getAllChannels,
  addUserChannel,
  deleteUserChannel,
} from "@/api/channel";
import { mapState } from "vuex";
import { setItem } from "@/utils/storage.js";
export default {
  name: "ChannelEdit",
  props: {
    userChannels: {
      type: Array,
      required: true,
    },
    activeIndex: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      allChannels: [],
      isEdit: false,
      fiexdChannels: [0],
    };
  },
  created() {
    this.loadAllChannels();
  },
  methods: {
    async loadAllChannels() {
      try {
        const { data } = await getAllChannels();
        this.allChannels = data.data.channels;
      } catch (error) {
        console.log(error);
        this.$toast("获取所有频道数据失败");
      }
    },

    async onAddChannel(channel) {
      this.userChannels.push(channel);
      if (this.user) {
        try {
          await addUserChannel({
            id: channel.id,
            seq: this.userChannels.length,
          });
        } catch (error) {
          this.$toast("保存失败，请稍后再试");
        }
      } else {
        // 未登录，把数据存储到本地
        setItem("TOUTIAO_CHANNELS", this.userChannels);
      }
    },
    onMyChannelClick(channe, index) {
      console.log(channe);
      if (this.isEdit) {
        if (this.fiexdChannels.includes(channe.id)) {
          return;
        }
        this.userChannels.splice(index, 1);
        if (index <= this.activeIndex) {
          this.$emit("onUpdateActive", this.activeIndex - 1, true);
        }
        this.deleteChannel(channe);
      } else {
        // 非编辑状态，指向切换频道
        this.$emit("onUpdateActive", index, false);
      }
    },

    async deleteChannel(channe) {
      try {
        if (this.user) {
          await deleteUserChannel(channe.id);
        } else {
          // 未登录，将数据更新到本地
          setItem("TOUTIAO_CHANNELS", this.userChannels);
        }
      } catch (error) {
        this.$toast("操作失败，请稍后重试");
      }
    },
  },
  computed: {
    ...mapState(["user"]),
    recommendChannele() {
      return this.allChannels.filter((channel) => {
        return !this.userChannels.find((myChannel) => {
          return myChannel.id === channel.id;
        });
      });
    },
  },
};
</script>

<style scoped lang="less">
.channel-edit {
  padding: 85px 0;

  .title-text {
    font-size: 32px;
    color: #333;
  }

  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }

  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap;
      background-color: #f4f5f6;

      .van-grid-item__text {
        font-size: 22px;
        color: #222;
        margin-top: 4px !important;
      }
      .active {
        color: red;
      }
    }
  }
  /deep/.van-grid-item__icon-wrapper {
    position: unset;
  }

  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }

  /deep/ .recomment-grid {
    .grid-item {
      width: 160px;
      height: 86px;
      .van-grid-item__content {
        flex-direction: row;

        .van-icon-plus {
          font-size: 22px;
          line-height: 22px;
          margin-right: 6px;
        }
      }
    }
  }
  .van-grid-item__text,
  .text {
    font-size: 22px;
    color: #222;
    margin-top: 4px !important;
  }
}
</style>